package com.cq.hd.app.biz;

import com.alibaba.fastjson.JSONObject;
import com.cq.hd.common.constant.WxConstant;
import com.cq.hd.common.utils.wxpub.WX_HttpsUtil;
import com.cq.hd.common.utils.wxpub.menu.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @description: 微信公众号服务号的按钮业务
 * @author: mYunYu
 * @date: 2021/7/24 22:25
 * @version: v1.0
 */
@Component
@Slf4j
public class WeChatMenuBiz {

    public static void main(String[] args) {
        // 调用接口获取access_token
//        String token= weChatBiz.getWeChatAccessToken(WX_APP_ID, WX_APP_SECRET);
        String token = "77_AqOrQeQ394YqR7l6ZGkc83R-JoVNJL24_C-JYPfTab0sxe47FOttLXig8tD9sABsp1-gUKjOYxh56nL638xu0B1cyr5W5qEfgnbybC9zSFEF9Jz_IgmuoCWR3ZIKKYhAIAVVJ";
//        String gzhUrl = "https://s.click.ele.me/InbjKku";
        if (!StringUtils.isEmpty(token)) {
            // 调用接口创建菜单
            int result = createMenu(getMenu(), token);
            // 判断菜单创建结果
            if (0 == result)
                log.info("菜单创建成功！");
            else
                log.info("菜单创建失败，错误码：" + result);
        }
    }

    /**
     * @Description: 创建微信公众号菜单按钮
     * @Param: [menu, accessToken]
     * @Return: int
     * @Auther: mYunYu
     * @Date: 2021/7/24 23:01
     */
    public static int createMenu(Menu menu, String accessToken) {
        int result = 0;
        // 拼装创建菜单的url
        String url = WxConstant.WX_PUB_MENU_CREATE_API.replace("ACCESS_TOKEN", accessToken);
        // 将菜单对象转换成json字符串
        String jsonMenu = JSONObject.toJSONString(menu);
        // 调用接口创建菜单
        JSONObject jsonObject = WX_HttpsUtil.httpsRequest2(url, "POST", jsonMenu);
        if (null != jsonObject) {
            if (0 != jsonObject.getInteger("errcode")) {
                result = jsonObject.getInteger("errcode");
                log.error("创建菜单失败 errcode:{} errmsg:{}", jsonObject.getInteger("errcode"), jsonObject.getString("errmsg"));
            }
        }

        return result;
    }

    /**
     * @Description: 组装微信公众号菜单数据
     * @Param: [url]
     * @Return: com.cq.shop.goods.utils.wechat.menu.Menu
     * @Auther: mYunYu
     * @Date: 2021/7/24 22:31
     */
    public static Menu getMenu() {
        // 周边活动：火动小程序主页
        // 火动订单：进到小程序订单tab
        // 火动来答：①加入火动（商家入驻引导页面）；
        //         ②联系客服（小程序客服）

        MiniProgramButton miniBtn1 = new MiniProgramButton();
        miniBtn1.setName("周边活动");
        miniBtn1.setType("miniprogram");
        miniBtn1.setAppid("wx9de1c129019aaab3");
        miniBtn1.setPagepath("pages/home/home");
        miniBtn1.setUrl("http://mp.weixin.qq.com");

        MiniProgramButton miniBtn2 = new MiniProgramButton();
        miniBtn2.setName("火动订单");
        miniBtn2.setType("miniprogram");
        miniBtn2.setAppid("wx9de1c129019aaab3");
        miniBtn2.setPagepath("pages/home/home?tab=1");
        miniBtn2.setUrl("http://mp.weixin.qq.com");

        MiniProgramButton miniBtn3 = new MiniProgramButton();
        miniBtn3.setName("加入火动");
        miniBtn3.setType("miniprogram");
        miniBtn3.setAppid("wx9de1c129019aaab3");
        miniBtn3.setPagepath("/applyPages/inviteApply/inviteApply");
        miniBtn3.setUrl("http://mp.weixin.qq.com");

//        MiniProgramButton miniBtn4 = new MiniProgramButton();
//        miniBtn4.setName("联系客服");
//        miniBtn4.setType("");
//        miniBtn4.setAppid("");
//        miniBtn4.setPagepath("");
//        miniBtn4.setUrl("");

        ComButton mainBtn3 = new ComButton();
        mainBtn3.setName("火动来答");
        mainBtn3.setSub_button(new Button[]{miniBtn3});

        Menu menu = new Menu();
        menu.setButton(new Button[]{miniBtn1, miniBtn2, mainBtn3});
        return menu;
    }

}
